package com.lj.controller;

import com.github.pagehelper.PageInfo;
import com.lj.pojo.Clientlj;
import com.lj.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/client")
public class ClientAction {
    public static final int PAGE_SIZE=5;
    @Autowired
    ClientService clientService;
    @RequestMapping("/selectClient")
    public String selectClient(HttpServletRequest request){
        //得到第一页数据
        PageInfo info=clientService.selectClient(1,PAGE_SIZE);
        request.setAttribute("info",info);
        return "admin/clientView";
    }
    //ajax分页翻页处理
    @ResponseBody
    @RequestMapping("/ajaxSplit")
    public void ajaxSplit(int page, HttpSession session){
        //取得当前page参数页面的数据
        PageInfo info=clientService.selectClient(page,PAGE_SIZE);
        session.setAttribute("info",info);
    }
    @RequestMapping("/insert")
    public String insert(Clientlj clientlj){
        int num=-1;
        num=clientService.insertClient(clientlj);
        return "redirect:/client/selectClient.action";
    }
    @RequestMapping("/delete")
    public String delete(String clientId){
        int nums=-1;
        nums=clientService.delete(clientId);
        return "forward:/client/selectClient.action";
    }
    @ResponseBody
    @RequestMapping("/getClientById")
    public Clientlj getClientById(String clientId, HttpServletRequest request ){
        Clientlj clientlj=clientService.getById(clientId);
        return clientlj;
    }
    @RequestMapping("/update")
    public String update(Clientlj clientlj){

        int nums= clientService.update(clientlj);
        return "redirect:/client/selectClient.action";
    }

}
